import { Component, OnInit } from '@angular/core';
import { NzTreeNode, NzMessageService } from 'ng-zorro-antd';
import { ArrayService } from '@delon/util';
import { HttpService } from 'lbf';

@Component({
  selector: 'app-czy-role',
  templateUrl: './role.component.html',
})
export class CzyRoleComponent implements OnInit {

  // 角色列表
  data: any;
  loading = false;

  // 权限列表
  permission: NzTreeNode[];
  chklist: [];
  item = { title: '', key: '' };
  title = '请选择用户类型';
 

  constructor(private lbservice: HttpService, private msgSrv: NzMessageService, private arrSrv: ArrayService, ) { }

  ngOnInit() {

    this.getData();
    this.getPermission('');

  }

  private getData() {
    this.lbservice.lbservice('FR_qusertype', {}).then((resdata: any) => {

      if (resdata.code < 1) {

        this.msgSrv.error("获取用户类型失败,错误信息:" + resdata.errmsg);

      } else {
        this.data = resdata.message.list;
      }

    });


  }

  private getPermission(key: string) {

    this.lbservice.lbservice('FR_qrole', { para: { usertype: key } }).then((resdata: any) => {

      if (resdata.code < 1) {

        this.msgSrv.error("获取权限列表失败,错误信息:" + resdata.errmsg);

      } else {
        this.permission = resdata.message.list;
        this.chklist = resdata.message.chk;
      }

    });
  }


  show(item: any) {
    this.item = item;
    this.title = "【" + item.title + "】用户权限";
    this.getPermission(item.key);

  }

  save() {

    if (!this.item.key){
      this.msgSrv.error('请先选择用户类型！');
      return;
    }
     

    this.loading = true;

    const checkedItem = [];
    this.arrSrv.visitTree(this.permission, (i: any) => {
      console.log(i);
      if (i.checked && i.isLeaf) {
        checkedItem.push(i.key);
      }
    });

    this.lbservice.lbservice('FR_sroles',{para:{roles:checkedItem,usertype:this.item.key}}).then((resdata)=>{
      if (resdata.code < 1) {

        this.msgSrv.error("获取权限列表失败,错误信息:" + resdata.errmsg);

      } else {
        this.msgSrv.info("保存成功！");
        this.getPermission(this.item.key);
      }
  
    });


    this.loading = false;

  }

}
